﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SIMA.Generator
{
    public partial class Form1 : Form
    {
        SqlAccess access;
        List<DataBase> databases;
        DataBase selectedDatabase;
        List<Table> tablesFromDatabase;

        List<Table> selectedTables;

        public Form1()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            selectedTables = new List<Table>();
        }

        private void chkUseCredentials_CheckedChanged(object sender, EventArgs e)
        {
            if (chkUseCredentials.Checked)
            {
                txtPassword.Enabled = true;
                txtUserName.Enabled = true;
            }
            else
            {
                txtPassword.Enabled = false;
                txtUserName.Enabled = false;
            }
        }

        private void txtServerName_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = string.Empty;
            access = new SqlAccess(txtServerName.Text, txtUserName.Text, txtPassword.Text);
            databases = access.GetDataBases();
            foreach (DataBase db in databases)
            {
                cmbDataOrigins.Items.Add(db.Database_name);
            }
        }

        private void cmbDataOrigins_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedDatabase = databases.Where(c => c.Database_name == cmbDataOrigins.SelectedItem.ToString()).First();
            tablesFromDatabase = access.GetTablesFromDataBase(selectedDatabase);
            foreach (Table t in tablesFromDatabase)
            {
                lstAllTables.Items.Add(t.Name);
            }
            
        }

        private void btnAddToGenerator_Click(object sender, EventArgs e)
        {
            lstSelectedTable.Items.Add(lstAllTables.SelectedItem);
            lstAllTables.Items.Remove(lstAllTables.SelectedItem);
            if (lstSelectedTable.Items.Count > 0)
            {
                btnRemoveFromGenerator.Enabled = true;
                btnGenerator.Enabled = true;
            }
            if (lstAllTables.Items.Count == 0)
            {
                btnAddToGenerator.Enabled = false;
            }
        }

        private void btnGenerator_Click(object sender, EventArgs e)
        {
            string path = string.Empty;
            FolderBrowserDialog selectPath = new FolderBrowserDialog();
            selectPath.ShowDialog();
            path = selectPath.SelectedPath;

            foreach (var a in lstSelectedTable.Items)
            {
                selectedTables.Add(tablesFromDatabase.Where(x => x.Name == a.ToString()).First());
            }

            foreach (Table T in selectedTables)
            {
                csGenerator.CreateModelClass(T, selectedDatabase.Database_name, path);
                if (chkWcfEnable.Checked)
                {
                    csGenerator.CreateInterface(T, selectedDatabase.Database_name, path);
                    csGenerator.CreateService(T, selectedDatabase.Database_name, path);
                    csGenerator.CreateAsyncResult(T, selectedDatabase.Database_name, path);
                }
            }
            if (chkWcfEnable.Checked)
            {
                csGenerator.CreateBaseAsyncResult(selectedDatabase.Database_name, path);
            }
            MessageBox.Show("Generación de entidades terminada.");
        }
    }
}
